{
 Mugen Character Def ( Definitions ) File Support

 @author Eduardo "Trialforce" Bonfandini
 @site http://trialforce.nostaljia.eng.br
 @email trialforce@gmail.com
}

unit DefChar;

interface

uses
  Mugen, SysUtils, Classes,dialogs;
type

TInfo=record
Name:string;
displayName:string;
versiondate:string;
mugenversion:string;
author:string;
paldefaults:string;
localcoord:string;
end;

TFiles=record
cmd:string;
cns:string;
sprite:string;
anim:string;
ai:string;
sound:string;
stcommon:string;
st: array of string;
pal: array of string;
end;

TArcade=record
introstoryboard:string;
endingstoryboard:string;
end;

TDefChar = class
private
    { Private declarations }
protected
   { Protected declarations }
public
Lines: TStringList;
Info:TInfo;
Files:Tfiles;
Arcade:Tarcade;

Constructor Create;
Procedure saveTofile(filename:string);
Procedure loadFromFile(filename:string);
published

end;

implementation

Constructor TDefChar.Create;
begin
    Lines := TStringList.create;
    setLength( files.pal , 13 );
    setLength( files.st , 9 );
    //Info.name:='';
    //Info.displayName:='';
    //info.versiondate:='';
    //info.mugenversion:='';
    //info.author:='';
    //info.paldefaults:='';
    //info.localcoord:='';
    //Files.cmd:='';
    //Files.cns:='';
    //Files.stcommon:='';
    //Files.sprite:='';
    //Files.anim:='';
    //Files.sound:='';
    //Arcade.introstoryboard:='';
    //Arcade.endingstoryboard:='';
end;

Procedure TDefChar.saveToFile(filename:string);
var
    i : integer;
begin
lines.clear;
lines.add('[Info]');
lines.add('name="'+info.name+'"');
lines.add('displayname="'+info.displayname+'"');
lines.add('versiondate='+info.versiondate);
lines.add('mugenversion='+info.mugenversion);
lines.add('author="'+info.author+'"');
lines.add('pal.defaults='+info.paldefaults);
lines.add('localcoord='+info.localcoord);

lines.add('');

lines.add('[Files]');
lines.add('cmd='+files.cmd);
lines.add('cns='+files.cns);
lines.add('st='+files.st[0]);
lines.add('stcommon='+Files.stcommon);

for i := 1 to high( files.st )  do
begin
    if files.st[i] <> '' then
    begin
        lines.add('st'+ intToStr( i ) + '=' + files.st[ i ] );
    end;
end;

lines.add('sprite='+Files.sprite);
lines.add('anim='+Files.anim);
lines.add('ai='+Files.ai);
if files.sound<>'' then lines.add('sound='+Files.sound);

for i := 1 to high( files.pal )  do
begin
    if files.pal[i]<> '' then
    begin
        lines.add('pal'+ intToStr( i) + '=' + files.pal[i] );
    end;
end;

lines.add('');

lines.add('[Arcade]');
if arcade.introstoryboard<>''then lines.add('intro.storyboard='+Arcade.introstoryboard);
if arcade.endingstoryboard<>''then lines.add('ending.storyboard='+Arcade.endingstoryboard);


lines.SaveToFile(filename);
end;

Procedure TDefChar.LoadFromFile(filename:string);
var
    str     : string;
    verify  : string;
    content : string;
    i       : integer;
    palI    : integer;
    stI     : integer;
begin
    Lines.loadFromFile(filename);
    i:=0;
    while i<lines.Count do
    begin
        Str     := trim(semcoment(lines[i]));
        Verify  := getleft( trim (lowercase(semcoment(lines[i]))));
        content := trim(semaspas(copy(str,pos('=',str)+1,length(str))));

        if ( pos('displayname',verify) > 0 ) then
        begin
            Info.displayName:=content;
        end;

        if ( ( pos('displayname',verify) = 0 ) and ( pos('name',verify) > 0 ) ) then
        begin
            begin
                Info.Name:=content;
            end;
        end;

        if pos('versiondate',verify)>0 then
        begin
            Info.versiondate:=content;
        end;

        if pos('mugenversion',verify)>0 then
        begin
            Info.mugenversion:=content;
        end;

        if pos('author',verify)>0 then
        begin
            Info.author:=content;
        end;

        if pos('pal.defaults',verify)>0 then
        begin
            Info.paldefaults:=content;
        end;

        if pos('localcoord',verify)>0 then
        begin
            Info.localcoord := content;
        end;

        if pos('ending.storyboard',verify)>0 then
        begin
            Arcade.endingstoryboard:=content;
        end;

        if pos('intro.storyboard',verify)>0 then
        begin
            Arcade.introstoryboard:=content;
        end;

        if pos('cmd',verify)>0 then
        begin
            Files.cmd:=content;
        end;

        if pos('cns',verify)>0 then
        begin
            Files.cns:=content;
        end;

        if pos('sprite',verify)>0 then
        begin
            Files.sprite:=content;
        end;

        if pos('anim',verify)>0 then
        begin
            Files.anim:=content;
        end;

        if pos('ai',verify)>0 then
        begin
            Files.ai:=content;
        end;

        if pos('sound',verify)>0 then
        begin
            Files.sound:=content;
        end;

        if pos('stcommon',verify)=0 then
        if pos('st1',verify)=0 then
        if pos('st2',verify)=0 then
        if pos('st3',verify)=0 then
        if pos('st4',verify)=0 then
        if pos('st5',verify)=0 then
        if pos('st6',verify)=0 then
        if pos('st7',verify)=0 then
        if pos('st8',verify)=0 then
        if pos('story',verify)=0 then
        if pos('st',verify) > 0 then
        begin
            files.st[0] := content;
        end;

        if pos('stcommon',verify)>0 then
        begin
            files.stcommon:=content;
        end;

        for stI := 1 to high( files.st ) do
        begin
            if pos( 'st' + intToStr(sti) , verify ) > 0  then
            begin
                files.st[sti] := content;
            end;
        end;

        for palI := 1 to high( files.pal ) do
        begin
            if pos( 'pal' + intToStr(pali) , verify ) > 0  then
            begin
                files.pal[pali] := content;
            end;
        end;

inc(i,1);
end;
end;

end.
